import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

//集中管理多个组件共享数据
export default new Vuex.Store({
    //数据，状态
    state:{
        //模板中：{{$store.state.xxx}}
        //组件逻辑中：this.$store.state.xxx
        //js模块中：store.state.xxx
		userToken:uni.getStorageSync('userToken') ? uni.getStorageSync('userToken'):'',
		storeId:3,
		loginShow:uni.getStorageSync('user')? false : true,
		userInfo:uni.getStorageSync('user') ? uni.getStorageSync('user'):null,
		storeInfo:{},
		shoppingCartSum:0,
		delivery:0,
		remark:'',
		estimatedDeliveryTime:'',
		shoppingCartChange:true
    },
    getters:{
		userInfo(state){
			return state.userInfo;
		},
		token(state){
			if(state.userInfo != null)
				return state.userInfo.token;
			else
				return '';
		},
		storeInfo(state){
			return state.storeInfo;
		}
		
    },
    //修改共享数据，必须是同步操作
    mutations:{
		MLOGIN(state,userInfo){
			uni.setStorageSync('user',userInfo)
			state.userInfo = userInfo;
			state.loginShow = false;
		},
		MLOGOUT(state){
			uni.removeStorageSync('user')
			state.userInfo = '';
			state.loginShow = true;
		},
		STOREINFO(state,storeInfo){
			state.storeInfo = storeInfo;
		}
    },
    //异步操作
    actions:{
		login(context,userInfo){
			context.commit('MLOGIN',userInfo)
		},
		logout(context){
			context.commit('MLOGOUT')
		},
		storeInfo(context,storeInfo){
			context.commit('STOREINFO',storeInfo)
		}
    },
    modules:{

    }
})